﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace RedmineIssuesProvider
{
    public partial class SettingsForm : Form
    {
        public SettingsForm(Service service)
        {
            InitializeComponent();

            MainForm.FillComboBox(service.Users,cbUser);
            cbUser.SelectedItem = service.Users.Find(x => (x.id == Properties.Settings.Default.CurrentUserID));
            MainForm.FillComboBox(service.Projects, cbProject);
            cbProject.SelectedItem = service.Projects.Find(x => (x.id == Properties.Settings.Default.DefaultProject));
            MainForm.FillComboBox(service.Filters.Content,cbFilter);
            cbFilter.SelectedItem =
                service.Filters.Content.Find(x => (x.name == Properties.Settings.Default.DefaultFilter));
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void fmSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.CurrentUserID = ((User) cbUser.SelectedItem).id;
                Properties.Settings.Default.DefaultProject = ((Project) cbProject.SelectedItem).id;
                Properties.Settings.Default.DefaultFilter = cbFilter.Text;

                Properties.Settings.Default.Save();
            }
            else
            {
                Properties.Settings.Default.Reload();
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            MySqlConnection _connection;

            _connection = new MySqlConnection("Server=" + tbHost.Text + ";Database=" + tbDB.Text +
                "; User=" + tbLogin.Text + ";Password=" + tbPassword.Text + ";Charset=utf8;");

            try
            {
                _connection.Open();
                MessageBox.Show("Подключение установлено!","Подключение", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Не удалось подключиться к базе данных!\n"+ex.Message, "Ошибка", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                return;
            }
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            string oldPath = tbFiltersFile.Text;

            openFileDialog1.DefaultExt = "xml";
            openFileDialog1.Multiselect = false;
            openFileDialog1.CheckFileExists = false;

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbFiltersFile.Text = openFileDialog1.FileName;

                FilterList filterList = new FilterList();
                try
                {
                    filterList.GetAll(tbFiltersFile.Text);
                    MainForm.FillComboBox(filterList.Content,cbFilter);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Неправильно задан путь к фильтрам!\n" + ex.Message, "Ошибка", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                    tbFiltersFile.Text = oldPath;
                }
                
            }
        }
    }
}
